const {
  theClass,
  student,
  course,
  experimentCourse,
  teacher,
  laboratory,
  experimentPlan,
  experimentScore,
  experimentTotalScore
} = require('../data');

const { 
  TheClass,
  Student,
  Course,
  ExperimentCourse,
  Teacher,
  Laboratory,
  ExperimentPlan,
  ExperimentScore,
  ExperimentTotalScore
} = require('../model');

async function initSQLData() {
  // Promise.all() 方法接收一个 promise 的 iterable 类型
  // Array.prototype.map() 方法创建一个新数组
  // 这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成，若没 return 则默认 return undefine。
  await Promise.all(theClass.map(item => {
    return TheClass.create(item)
    .then(() => {
      console.log('TheClass 表初始化数据成功');
    })
    .catch(err => {
      console.log('TheClass 表初始化数据失败', err);
    });
  }));
  await Promise.all(student.map(item => {
    return Student.create(item)
    .then(() => {
      console.log('Student 表初始化数据成功');
    })
    .catch(err => {
      console.log('Student 表初始化数据失败', err);
    })
  }));

  await Promise.all(course.map(item => {
    return Course.create(item)
    .then(() => {
      console.log('Course 表初始化数据成功');
    })
    .catch(err => {
      console.log('Course 表初始化数据失败', err);
    });
  }));
  await Promise.all(experimentCourse.map(item => {
    return ExperimentCourse.create(item)
    .then(() => {
      console.log('ExperimentCourse 表初始化数据成功');
    })
    .catch(err => {
      console.log('ExperimentCourse 表初始化数据失败', err);
    });
  }));

  await Promise.all(teacher.map(item => {
    return Teacher.create(item)
    .then(() => {
      console.log('Teacher 表初始化数据成功');
    })
    .catch(err => {
      console.log('Teacher 表初始化数据失败', err);
    });
  }));

  await Promise.all(laboratory.map(item => {
    return Laboratory.create(item)
    .then(() => {
      console.log('Laboratory 表初始化数据成功');
    })
    .catch(err => {
      console.log('Laboratory 表初始化数据失败', err);
    });
  }));

  await Promise.all(experimentPlan.map(item => {
    return ExperimentPlan.create(item)
    .then(() => {
      console.log('ExperimentPlan 表初始化数据成功');
    })
    .catch(err => {
      console.log('ExperimentPlan 表初始化数据失败', err);
    });
  }));

  await Promise.all(experimentScore.map(item => {
    return ExperimentScore.create(item)
    .then(() => {
      console.log('ExperimentScore 表初始化数据成功');
    })
    .catch(err => {
      console.log('ExperimentScore 表初始化数据失败', err);
    });
  }));

  await Promise.all(experimentTotalScore.map(item => {
    return ExperimentTotalScore.create(item)
    .then(() => {
      console.log('ExperimentTotalScore 表初始化数据成功');
    })
    .catch(err => {
      console.log('ExperimentTotalScore 表初始化数据失败', err);
    });
  }));
}

// initSQLData();
module.exports = initSQLData;
